package com.lyc.iotdb.mapper.impl;

import cn.hutool.core.util.ObjectUtil;
import com.lyc.iotdb.mapper.WeatherMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 添加说明
 *
 * @author kisang
 * @version 1.0
 * @date 2023/11/27 17:21
 */
@Slf4j
@Component
public class WeatherMapperImpl implements WeatherMapper {
    /**
     * 分页查询List
     *
     * @param params 参数
     * @return sql
     */
    @Override
    public String getPageList(Map<String, Object> params) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select * from root.ln.weather where 1=1 ");
        if (ObjectUtil.isNotEmpty(params.get("startTime"))) {
            sqlBuilder.append(" and time >= ").append(params.get("startTime"));
        }
        if (ObjectUtil.isNotEmpty(params.get("endTime"))) {
            sqlBuilder.append(" and time <= ").append(params.get("endTime"));
        }
        if (ObjectUtil.isNotEmpty(params.get("quality"))) {
            sqlBuilder.append(" and quality = '").append(params.get("quality")).append("'");
        }
        if (ObjectUtil.isNotEmpty(params.get("temperature"))) {
            sqlBuilder.append(" and temperature >= ").append(params.get("temperature"));
        }
        if (ObjectUtil.isNotEmpty(params.get("remark"))) {
            sqlBuilder.append(" and remark like '%").append(params.get("remark")).append("%'");
        }
        sqlBuilder.append(" order by time desc ");
        return sqlBuilder.toString();
    }

    /**
     * 分页查询Map
     *
     * @param params 参数
     * @return sql
     */
    @Override
    public String getPageMap(Map<String, Object> params) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("select samplingTimeStr as samplingTimeStr, ");
        sqlBuilder.append(" cityKey as cityKey,");
        sqlBuilder.append(" city as city,");
        sqlBuilder.append(" temperature as temperature,");
        sqlBuilder.append(" humidity as humidity,");
        sqlBuilder.append(" pm10 as pm10,");
        sqlBuilder.append(" pm25 as pm25,");
        sqlBuilder.append(" quality as quality,");
        sqlBuilder.append(" remark as remark");
        sqlBuilder.append(" from root.ln.weather ");
        // sqlBuilder.append(" where 1=1 "); // 此处使用1=1 会报错StatementExecutionException: 701: Constant column is not supported.
        if (ObjectUtil.isNotEmpty(params.get("startTime"))) {
            sqlBuilder.append(" where time >= ").append(params.get("startTime"));
        }
        if (ObjectUtil.isNotEmpty(params.get("endTime"))) {
            sqlBuilder.append(" and time <= ").append(params.get("endTime"));
        }
        if (ObjectUtil.isNotEmpty(params.get("quality"))) {
            sqlBuilder.append(" and quality = '").append(params.get("quality")).append("'");
        }
        if (ObjectUtil.isNotEmpty(params.get("temperature"))) {
            sqlBuilder.append(" and temperature >= ").append(params.get("temperature"));
        }
        if (ObjectUtil.isNotEmpty(params.get("remark"))) {
            sqlBuilder.append(" and remark like '%").append(params.get("remark")).append("%'");
        }
        sqlBuilder.append("order by time desc ");
        return sqlBuilder.toString();
    }

    /**
     * 删除
     *
     * @param params 参数
     * @return sql
     */
    @Override
    public String delete(Map<String, Object> params) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("delete from root.ln.weather.* where ");
        if (ObjectUtil.isNotEmpty(params.get("startTime"))) {
            sqlBuilder.append(" time >= ").append(params.get("startTime"));
        }
        if (ObjectUtil.isNotEmpty(params.get("endTime"))) {
            sqlBuilder.append(" and time <= ").append(params.get("endTime"));
        }
        return sqlBuilder.toString();
    }
}
